<template>
<div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
        <el-form :inline="true" :model="filters" :size="size" ref="filters">
            <el-row class="search-input">
                <el-form-item prop="merchantIds" label="所属商家：" label-width="96px">
                    <el-select v-model="filters.merchantIds" style="width: 200px;" placeholder="所属商家">
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="status" label="销售渠道：">
                    <el-select v-model="filters.storeChannels" placeholder="销售渠道">
                        <el-option label="BBC" value="1"></el-option>
                        <el-option label="天猫" value="2"></el-option>
                        <el-option label="京东" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="storeCode" label="门店编码：">
                    <el-input v-model="filters.storeCode" placeholder="门店编码"></el-input>
                </el-form-item>
                <el-form-item prop="storeName" label="门店名称：">
                    <el-input v-model="filters.storeName" placeholder="门店名称"></el-input>
                </el-form-item>
                <el-form-item prop="storeContactor" label="门店联系人：">
                    <el-input v-model="filters.storeContactor" placeholder="门店联系人"></el-input>
                </el-form-item>
                <el-form-item prop="mobile" label="联系人手机：">
                    <el-input v-model="filters.mobile" placeholder="联系人手机"></el-input>
                </el-form-item>
                <el-form-item label="注册时间：" label-width="87px">
                    <el-date-picker v-model="filters.startDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="注册时间起" style="width:130px"></el-date-picker><span class="line">-</span>
                    <el-date-picker v-model="filters.endDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="注册时间止" style="width:130px"></el-date-picker>
                </el-form-item>
            </el-row>
            <el-row class="search-button">
                <el-form-item>
                    <kt-button icon="fa fa-search" :label="$t('action.search')" type="primary" @click="findPage(null)" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')" />
                </el-form-item>
                <el-form-item>
                    <kt-button icon="fa fa-plus" :label="$t('action.add')" type="primary" @click="handleAdd" v-if="showAdd" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" size="mini" @click="reviewStatus">批量审核</el-button>
                </el-form-item>
            </el-row>
            <el-form-item class="t_r w100">
                <el-button-group>
                    <el-tooltip content="刷新" placement="top">
                        <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="列显示" placement="top">
                        <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
                    </el-tooltip>
                    <el-tooltip content="导出" placement="top">
                        <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
                    </el-tooltip>
                </el-button-group>
            </el-form-item>
            <table-column-filter-dialog ref="tableColumnFilterDialog" :columns="columns" @handleFilterColumns="handleFilterColumns"></table-column-filter-dialog>
        </el-form>
    </div>
    <!--表格内容栏-->
    <Jic-table :height="350" :tableList="pageResult" :tableHeaderList="filterColumns" @findPage="findPage" @selectionChange="selectionChange" :showBatchDelete="false" showDetailOperation :showDeleteOperation="false" :showEditOperation="isShow" @handleDetail="handleDetail" @handleEdit="handleEdit" :totalSize="totalSize" @handlePageChange="handlePageChange" ref="JicTable"></Jic-table>.
    <!--新增编辑界面-->
    <el-dialog title="添加门店" width="40%" :visible.sync="editDialogVisible" :close-on-click-modal="false">
        <el-form :model="dataForm" label-width="100px" :rules="dataFormRules" ref="dataForm" :size="size">
            <el-row class="searchDialog-input">
                <el-form-item label="所属商家：" class="styleReplace2" prop="deptId" label-width="200px" v-if="deptType == '0'">
                    <el-select v-model="dataForm.deptId" style="width: 100%;" placeholder="所属商家" @change="isChose($event)">
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店编码：" prop="storeCode" label-width="200px">
                    <el-input v-model="dataForm.storeCode" auto-complete="off" :disabled="false" placeholder="请输入门店编码"></el-input>
                </el-form-item>
                <el-form-item label="门店名称：" prop="storeName" label-width="200px">
                    <el-input v-model="dataForm.storeName" auto-complete="off" :disabled="false" placeholder="请输入门店名称"></el-input>
                </el-form-item>
                <el-form-item label="门店名称（第二语言）：" prop="storeNames" label-width="200px">
                    <el-input v-model="dataForm.storeNames" auto-complete="off" :disabled="false" placeholder="支持填写1-30个字符" maxlength="30"></el-input>
                </el-form-item>
                <!--<el-form-item label="门店分类" prop="storeType" label-width="200px">-->
                <!--<el-select-->
                <!--v-model="dataForm.storeType"-->
                <!--placeholder="门店分类"-->
                <!--style="width:100%"-->
                <!--:disabled="storeDis"-->
                <!--&gt;-->
                <!--<el-option label="线上门店" value="1"></el-option>-->
                <!--</el-select>-->
                <!--</el-form-item>-->
                <!--<el-form-item label="所属组织：" prop="deptId" label-width="200px">-->
                <!--<el-select-->
                <!--v-model="dataForm.deptId"-->
                <!--placeholder="商家状态"-->
                <!--style="width:100%;"-->
                <!--@change="queryRoleList(dataForm.deptId)"-->
                <!--&gt;-->
                <!--<el-option-->
                <!--v-for="(item,index) in deptList"-->
                <!--:key="index"-->
                <!--:label="item.deptName"-->
                <!--:value="item.id"-->
                <!--&gt;</el-option>-->
                <!--</el-select>-->
                <!--</el-form-item>-->
                <el-form-item label="门店账号：" prop="storeAccount" label-width="200px">
                    <el-input v-model="dataForm.storeAccount" auto-complete="off" :disabled="false" placeholder="请输入门店账号"></el-input>
                </el-form-item>
                <el-form-item label="门店密码：" prop="storePass" label-width="200px">
                    <el-input v-model="dataForm.storePass" auto-complete="off" :disabled="false" placeholder="请输入门店密码"></el-input>
                </el-form-item>
                <el-form-item label="角色：" prop="roleId" label-width="200px">
                    <el-select v-model="dataForm.roleId" style="width:100%;" placeholder="请选择">
                        <el-option v-for="(item,index) in roleList" :key="index" :label="item.roleName" :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="销售渠道：" prop="storeChannels" label-width="200px">
                    <el-select v-model="dataForm.storeChannels" placeholder="销售渠道" style="width:100%;" :disabled="channelDis">
                        <el-option label="BBC" value="1"></el-option>
                        <el-option label="天猫" value="2"></el-option>
                        <el-option label="京东" value="3"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="门店联系人：" prop="storeContactor" label-width="200px">
                    <el-input v-model="dataForm.storeContactor" auto-complete="off" :disabled="false" @input="characterVer($event)" placeholder="请输入门店联系人"></el-input>
                </el-form-item>
                <el-form-item label="联系人手机号：" prop="mobile" label-width="200px">
                    <el-input v-model="dataForm.mobile" auto-complete="off" :disabled="false" @input="phoneVer($event)" maxlength="11" placeholder="请输入联系人手机号"></el-input>
                </el-form-item>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:center">
            <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" @click.native="submitForm" :loading="editLoading">{{$t('action.submit')}}</el-button>
        </div>
    </el-dialog>
    <!--审核界面-->
    <el-dialog title="入驻商家审核" width="30%" :visible.sync="reviewFormVisible" :close-on-click-modal="false">
        <el-form :model="reviewForm" label-width="100px" :rules="reviewFormRules" ref="reviewForm" :size="size">
            <el-row class="searchDialog-input">
                <el-form-item label="审核状态：" class="styleReplace2" prop="reviewStatus">
                    <el-select v-model="reviewForm.reviewStatus" style="width:100%">
                        <el-option label="审核通过" value="1">审核通过</el-option>
                        <el-option label="审核拒绝" value="2">审核拒绝</el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注：" class="reviewReason" prop="reviewReason">
                    <el-input v-model="reviewForm.reviewReason" auto-complete="off" placeholder="请输入备注"></el-input>
                </el-form-item>
            </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align:center">
            <el-button :size="size" @click.native="cancleReview">{{$t('action.cancel')}}</el-button>
            <el-button :size="size" type="primary" @click.native="reviewSuccess" :loading="editLoading">{{$t('action.submit')}}</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
import JicTable from "@/components/jicComponentGallery/jicTable";
import KtButton from "@/views/Core/KtButton";
import {
    format
} from "@/utils/datetime";
import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

export default {
    components: {
        JicTable,
        KtButton,
        TableColumnFilterDialog
    },
    data() {
        return {
            deptType: JSON.parse(localStorage.getItem("user")).deptType, //组织类型
            totalSize: 0, //初始化列表分页
            reviewFormRules: {
                replyContent: [{
                    required: true,
                    message: "拒绝原因",
                    trigger: "blur"
                }]
            },
            reviewForm: {
                reviewReason: "",
                reviewStatus: ""
            },
            reviewFormVisible: false,

            isShow: true,
            showAdd: true,
            // 新增编辑界面数据
            dataForm: {
                merchantId: "",
                storeType: "",
                storeChannels: "",
                storeCode: "",
                storeName: "",
                deptId: "",
                storeContactor: "",
                mobile: "",
                storeAccount: "",
                storePass: ""
            },
            content: "",
            editorOption: {
                modules: {
                    imageDrop: true
                },
                theme: "snow"
            },
            storeDis: true,
            channelDis: true,
            size: "small",
            filters: {
                merchantId: "",
                storeType: "",
                storeChannels: "",
                storeCode: "",
                storeName: "",
                startDate: "",
                endDate: ""
            },
            columns: [],
            filterColumns: [],
            pageRequest: {
                pageNum: 1,
                pageSize: 10
            },
            pageResult: [],
            options: {},
            operation: false, // true:新增, false:编辑
            editDialogVisible: false, // 新增编辑界面是否显示
            editLoading: false,
            dataFormRules: {
                deptId: [{
                    required: JSON.parse(localStorage.getItem("user")).deptType == 0 ? true : false,
                    message: "请选择所属商家",
                    trigger: "change"
                }],
                storeCode: [{
                    required: true,
                    message: "请输入编码",
                    trigger: "blur"
                }],
                storeAccount: [{
                    required: true,
                    message: "请输入门店账号",
                    trigger: "blur"
                }],
                storePass: [{
                    required: true,
                    message: "请输入门店密码",
                    trigger: "blur"
                }],
                storeContactor: [{
                    required: true,
                    message: "请输入门店联系人",
                    trigger: "blur"
                }],
                mobile: [{
                    required: true,
                    message: "请输入联系人手机号",
                    trigger: "blur"
                }],
                // storeType: [
                //   { required: true, message: "请选择门店分类", trigger: "blur" }
                // ],
                storeChannels: [{
                    required: true,
                    message: "请选择门店渠道",
                    trigger: "blur"
                }],
                storeName: [{
                    required: true,
                    message: "请输入门店名称",
                    trigger: "blur"
                }],
                // deptId: [
                //   { required: true, message: "请选择所属组织", trigger: "change" }
                // ],
                roleId: [{
                    required: true,
                    message: "请选择账号角色",
                    trigger: "change"
                }]
            },
            // 新增编辑界面数据
            dataForm: {
                merchantId: "",
                storeType: "",
                deptId: "",
                roleId: ""
            },
            shopList: [],
            statusList: [],
            checked: [],
            deptList: [],
            merchantDeptId: "",
            roleList: []
        };
    },
    methods: {
        //获取组织结构
        getDeptListByType() {
            var that = this;
            this.utils.request.queryDeptList({
                    deptType: 3,
                    pid: this.merchantDeptId
                },
                res => {
                    if (res.code == "0000") {
                        that.deptList = res.data;
                    }
                }
            );
        },
        // 查询角色列表
        queryRoleList() {
            this.utils.request.queryRoleListByDeptId({}, res => {
                this.roleList = res.data;
            });
        },
        //输入框验证

        characterVer(e) {
            //汉字验证
            const correct = e.replace(/[^\u4E00-\u9FA5]/g, ""); // 清除除了中文以外的输入的字符

            this.dataForm.storeContactor = correct;
        },

        phoneVer(e) {
            //手机号验证
            let filtered = e.replace(/^0|[^\d]/g, "");
            if (this.dataForm.mobile != filtered) {
                this.$message({
                    message: "请输入正确的手机号",
                    type: "error"
                });
                this.dataForm.mobile = "";
            }
        },

        //左右分页点击

        handlePageChange(params) {
            if (params != null && params.number != null) {
                if (params.number != null) {
                    this.pageRequest.pageNum = params.number;
                }

                this.findPage();
            }
        },
        //取消批量审核弹窗
        cancleReview: function () {
            let this_ = this;
            this_.reviewFormVisible = false;
            this_.selections = [];
            this_.findPage();
        },
        // 显示批量审核弹窗
        reviewStatus: function () {
            if (this.checked.length < 1) {
                this.$message({
                    message: "必须选中一条数据",
                    type: "error"
                });
                return false;
            }
            console.log(this.checked);
            this.reviewFormVisible = true;
            //this.resetForm("reviewForm");
        },
        // 批量审核
        reviewSuccess() {
            let boo = false;
            this.checked.forEach(selection => {
                if (selection.reviewStatus != 0) {
                    boo = true;
                }
            });
            if (boo) {
                this.$message({
                    message: "只能审核状态为【待审核】的用户",
                    type: "error"
                });
                return;
            }
            this.$refs.reviewForm.validate(valid => {
                if (valid) {
                    let params = Object.assign({}, this.reviewForm);
                    let this_ = this;
                    params.ids = this.checked.map(item => item.id).toString();
                    this.utils.request.reviewStoreStatus(params, res => {
                        if (res.code == "9999") {
                            this_.$message({
                                message: "审核错误,请检查是否填入审核原因",
                                type: "error"
                            });
                            this_.$refs["reviewForm"].resetFields();
                            this_.reviewFormVisible = false;
                            this_.selections = [];
                            this_.findPage();
                        } else {
                            this_.$message({
                                message: "审核成功",
                                type: "success"
                            });
                            this_.$refs["reviewForm"].resetFields();
                            this_.reviewFormVisible = false;
                            this_.selections = [];
                            this_.findPage();
                        }
                    });
                }
            });
        },
        //所属商家判断
        isChose: function (data) {
            let this_ = this;

            if (data != null) {
                this_.storeDis = false;
                this_.channelDis = false;
                for (var i = 0; i < this_.options.length; i++) {
                    if (this_.options[i].id == data) {
                        this_.merchantDeptId = this_.options[i].deptId;
                    }

                }
                this.getDeptListByType();
            } else {
                this_.storeDis = true;
                this_.channelDis = true;
            }
        },
        // 获取分页数据

        findPage: function (data) {
            /**
             * 子组件传过来的分页参数
             * data.start 当前页
             * data.limit 当前页总条数
             * */
            if (data != null && data.start != null) {
                if (data.start != null) {
                    this.pageRequest.pageNum = data.start;
                }
                if (data.limit != null) {
                    this.pageRequest.pageSize = data.limit;
                }
            }

            let this_ = this;
            if (this_.filters == undefined || this_.filters == null) {
                this_.filters = {};
            }
            this_.filters.start = this.pageRequest.pageNum;
            this_.filters.limit = this.pageRequest.pageSize;
            this_.utils.request.queryStoreListPage(this_.filters, function (res) {
                if (res.data != null) {
                    this_.pageResult = res.data.rows;
                    this_.totalSize = Number(res.data.total);
                }
            });
        },
        // 显示新增界面
        handleAdd: function () {
            //  let isDetail = 0
            //  let storeId = 0
            //  let merchantId = 0
            // this.$router.push({
            //   path: "/StoreInfoEdit/"+storeId+"/"+merchantId+"/"+isDetail,
            //   // query: { cid: data.row.id,merchantid:data.row.merchantId }
            // });
            this.queryRoleList();
            this.editDialogVisible = true;
            this.operation = true;
            this.resetForm("dataForm");
        },
        //选择框
        selectionChange: function (params) {
            console.log(params);
            this.checked = params.selections;
        },
        // 编辑页面
        handleEdit: function (data) {
            let isDetail = 0;
            this.$router.push({
                path: "/StoreInfoEdit/" +
                    data.row.id +
                    "/" +
                    data.row.merchantId +
                    "/" +
                    isDetail
                // query: { cid: data.row.id,merchantid:data.row.merchantId }
            });
        },
        // 详情页面
        handleDetail: function (data) {
            let isDetail = 1;
            this.$router.push({
                path: "/StoreInfoEdit/" +
                    data.row.id +
                    "/" +
                    data.row.merchantId +
                    "/" +
                    isDetail
                // query: { cid: data.row.id,isDetail:1}
            });
        },
        // 编辑
        submitForm: function () {
            this.$refs.dataForm.validate(valid => {
                if (valid) {
                    this.$confirm("确认提交吗？", "提示", {}).then(() => {
                        let params = Object.assign({}, this.dataForm);
                        let this_ = this;
                        this.utils.request.editStoreInfo(params, function (res) {
                            if (res.code == "0000") {
                                this_.$message({
                                    message: "操作成功",
                                    type: "success"
                                });

                                this_.$refs["dataForm"].resetFields();

                                this_.editDialogVisible = false;

                                this_.findPage(null);
                            } else {
                                this_.$message({
                                    message: "操作失败, " + res.msg,
                                    type: "error"
                                });
                            }
                        });
                    });
                }
            });
        },
        //商家初始化
        queryShopName() {
            var that = this;
            this.utils.request.queryMerchantListOther({}, function (data) {
                that.options = data.data;
            });
        },
        // 时间格式化
        dateFormat: function (row, column, cellValue, index) {
            return format(row[column.property]);
        },

        resetForm(formName) {
            if (this.$refs[formName] != undefined) {
                this.$refs[formName].resetFields();
                this.filters.startDate = "";
                this.filters.endDate = "";
                this.filters.storeChannels = "";
                this.findPage(null);
            }
        },

        // 处理表格列过滤显示
        displayFilterColumnsDialog: function () {
            this.$refs.tableColumnFilterDialog.setDialogVisible(true);
        },
        // 处理表格列过滤显示
        handleFilterColumns: function (data) {
            this.filterColumns = data.filterColumns;
            this.$refs.tableColumnFilterDialog.setDialogVisible(false);
        },
        // 处理表格列过滤显示
        initColumns: function () {
            this.columns = [{
                    prop: "storeCode",
                    label: "门店编码",
                    minWidth: 120
                },
                {
                    prop: "storeName",
                    label: "门店名称",
                    minWidth: 120
                },
                {
                    prop: "merchantName",
                    label: "所属商家	",
                    minWidth: 120
                },
                // {
                //   prop: "storeType",
                //   label: "门店分类",
                //   minWidth: 70,
                //   formatter: function(row, column, cellValue, index) {
                //     if (cellValue == 1) {
                //       return "线上门店";
                //     }
                //   }
                // },
                {
                    prop: "channelName",
                    label: "销售渠道",
                    minWidth: 70
                },
                {
                    prop: "addressDetail",
                    label: "门店地址",
                    minWidth: 120
                },
                {
                    prop: "storeContactor",
                    label: "门店联系人",
                    minWidth: 120
                },
                {
                    prop: "mobile",
                    label: "联系人手机",
                    minWidth: 80
                },
                {
                    prop: "createTime",
                    label: "注册时间",
                    minWidth: 120
                },
                {
                    prop: "reviewStatus",
                    label: "审核状态",
                    minWidth: 70,
                    formatter: function (row, column, cellValue, index) {
                        if (cellValue == 0) {
                            return "待审核";
                        }
                        if (cellValue == 1) {
                            return "审核通过";
                        }
                        if (cellValue == 2) {
                            return "审核拒绝";
                        }
                    }
                }
            ];
            var temp = [];

            $.each(this.columns, function (key, val) {
                temp.push(val);
            });
            this.filterColumns = temp;
        },
        //列表下载
        downloadExcel() {
            this.$confirm("确定下载列表文件?", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                })
                .then(() => {
                    let filename = "换货管理下载";
                    this.$refs.JicTable.exportData(
                        this.filters,
                        this.filterColumns,
                        filename
                    );
                })
                .catch(() => {});
        }

        // //获取登录用户并判断
        // getLoginUser(){
        //    let this_= this;
        //   this.utils.request.getLoginUser({}, function(data) {

        //     if(data.data.roleid==1){

        //      this_.showAdd=true;
        //     this_.isShow=true;
        //     }
        //     if(data.data.roleid==26){true
        //         this_.showAdd=true;
        //         this_.isShow=true;
        //     }
        //   });
        // }
    },
    mounted() {
        this.initColumns();
        this.findPage();
        this.queryShopName();
    }
};
</script>

<style lang="scss" scoped>
.line {
    margin: 0 10px;
}
</style>
